package com.zzh.server.controller;

import com.zzh.server.pojo.Admin;
import com.zzh.server.pojo.AdminLogin;
import com.zzh.server.pojo.baseBean;
import com.zzh.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/*
 * @Auther : wenrouyue
 * @Date : 2022/4/8-0:24
 */
@RestController
@Slf4j
@Api(tags = "LoginController")
public class LoginController {

    @Resource
    private IAdminService adminService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public baseBean login(@RequestBody AdminLogin adminLogin, HttpServletRequest request){
        log.info("/login ==> ");
        return adminService.login(adminLogin.getUsername(),adminLogin.getPassword(),request);
    }
    @ApiOperation(value = "获取当前登录信息")
    @PostMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if (null == principal){
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUsername(username);
        admin.setPassword(null);
        log.info("/admin/info ==> ");
        return admin;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public baseBean logout(){
        log.info("/logout ==> ");
        return baseBean.success("退出成功!");
    }
}
